package com.tedu._03vehicle.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu._03vehicle.base.response.JsonResult;
import com.tedu._03vehicle.base.response.StatusCode;
import com.tedu._03vehicle.mapper.VehicleMapper;
import com.tedu._03vehicle.pojo.dto.VehicleAddParam;
import com.tedu._03vehicle.pojo.dto.VehicleListQuery;
import com.tedu._03vehicle.pojo.entity.Vehicle;
import com.tedu._03vehicle.pojo.vo.UserVO;
import com.tedu._03vehicle.pojo.vo.VehicleListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("v1/vehicle")
@Tag(name="2-车辆模块")
@Validated
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;
    @PostMapping("/add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session){
        log.debug("vehicleAddParam="+vehicleAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setUserId(userVO.getId());
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        int num = vehicleMapper.insert(vehicle);
        return num>0?JsonResult.success():new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @GetMapping("/list")
    @Operation(summary = "车辆列表")
    @Parameters({
            @Parameter(name = "brand",required = true),
            @Parameter(name = "license",required = true),
            @Parameter(name = "vehicleListQuery",required = true,hidden = true)
    })
    public JsonResult select(VehicleListQuery vehicleListQuery,HttpSession session){
        log.debug("vehicleListQuery="+vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        List<VehicleListVO> vehicleListVO = vehicleMapper.select(brand,license,userVO.getId());
        return JsonResult.success(vehicleListVO);
    }
    @PostMapping("/del/{id}")
    @Operation(summary = "删除车辆")
    public JsonResult delete(@PathVariable @Range(min =1,max=500,message="车辆编号不合法") Long id, HttpSession session) {
        if (id < 0) {
        throw new IllegalArgumentException("车辆id不能小于0");
    }

        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }

        int num = vehicleMapper.delete(id, userVO.getId());
        return num>0?JsonResult.success():new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
